<style scoped>
.company>>>.layui-form-select {
    z-index: auto;
}
</style>

<template>
<div class="company">
    <fieldset class="layui-elem-field layui-field-title">
        <legend>修改产品</legend>
    </fieldset>
    <form class="layui-form ">
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">套装名称</label>
            <div class="layui-input-block">
                <select type="text" name="suitSpid" id="category" lay-filter="category" autocomplete="off" lay-verify="required" class="layui-select" lay-search>
                    <option value="">请选择</option>
                </select>
            </div>
        </div>

        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">单品名称</label>
            <div class="layui-input-block">
                <select type="text" name="childSpid" id="brand" lay-filter="productGuid" autocomplete="off" lay-verify="required" class="layui-select" lay-search>
                    <option value="">请选择</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">数量</label>
            <div class="layui-input-block">
                <input type="text" name="amount" v-model="adata.amount" required lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">单价</label>
            <div class="layui-input-block">
                <input type="text" name="price" v-model="adata.price" required lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="submit">立即提交</button>
                <button class="layui-btn layui-btn-primary" @click="$parent.back">返回</button>
            </div>
        </div>
        <div style="height: 200px"></div>
    </form>
</div>
</template>

<script>
let qs = require('qs');

export default {
    name: 'editSalesSpecification',
    data() {
        return {
            guid: "",
            adata: {},
        }
    },
    mounted() {
        let that = this;
        that.guid = that.$parent.guids
        this.getBaseInfo()
        this.getBaseInfo1()
        layui.use(['form'], function () {
            var form = layui.form,
                layer = layui.layer;
            let loader = layer.load();
            //获取回显数据
            $.ajax({
                url: 'sales/Suitpack!getDetail.action',
                data: {
                    suitpackid: that.guid
                },
                type: "post",
                dataType: "json",
                success: function (res) {
                    console.log("消炎药回显数据", res.pack)
                    that.adata = res.pack;
                    form.render('select');
                },
                complete: function () {
                    layer.close(loader);
                }
            });
            //提交编辑过的数据
            form.on('submit(submit)', function (data) {
                data.field.guid = that.guid;
                let params = {  
                    'entity.guid': that.guid,
                    'entity.amount': data.field.amount,
                    'entity.childSpid': data.field.childSpid,
                    'entity.price': data.field.price,
                    'entity.suitSpid': data.field.suitSpid,
                }
                console.log(params)
                let loader = layer.load();
                $.ajax({
                    url: 'sales/Suitpack!update.action',
                    data: params,
                    type: "post",
                    dataType: "json",
                    success: function (res) {
                        that.toast("修改成功");
                        that.$parent.switchPage('SuitPack');
                    },
                    complete: function () {
                        layer.close(loader);
                    }
                });
                return false;
            });
        });

    },
    updated() {
        layui.form.render('select');
        layui.form.render();
    },
    methods: {
        back: function () {
            this.$parent.switchPage(this.$parent.refView);
        },
        getBaseInfo: function (type = '') {
            let params = {
                'entity.flag': 2
            }
            $.ajax({
                url: '/sales/Salesspecification!getSpecification.action',
                type: "post",
                dataType: "json",
                data: params,
                success: function (res) {
                    console.log(res)
                    $.each(res.spes, function (index, item) {
                        $('#category').append(new Option(item.name, item.guid));
                        layui.form.render("select");
                    });
                }
            });
        },
        getBaseInfo1: function (type = '') {
            let params = {
                'entity.flag': 1,
                'entity.type': 2
            }
            $.ajax({
                url: '/sales/Salesspecification!getSpecification.action',
                type: "post",
                dataType: "json",
                data: params,
                success: function (res) {
                    console.log(res)
                    $.each(res.spes, function (index, item) {
                        $('#brand').append(new Option(item.name, item.guid));
                        layui.form.render("select");
                    });
                }
            });
        }
    }
}
</script>
